﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.Maintenance;
using WaterRevenueMS.Core.Repository.Maintenance;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.Maintenance;
using System.Linq.Expressions;
using WaterRevenueMS.Domain.ViewModels.Customer.MeterInfo;
using WaterRevenueMS.Core.Repository.Customer;

namespace WaterRevenueMS.Core.Service.Maintenance.Impl
{

    [Export(typeof(IRepairRecordService))]
    public class RepairRecordService : CoreServiceBase, IRepairRecordService
    {
        [Import]
        public IRepairRecordRepository RepairRecordRepository { get; set; }

        [Import]
        public WaterRevenueMS.Core.Repository.Customer.IMeterInfoRepository MeterInfoRepository { get; set; }



        public IQueryable<Componnent.Entities.Maintenance.RepairRecord> RepairRecords
        {
            get { return RepairRecordRepository.Entities; }
        }

        public IQueryable<WaterRevenueMS.Componnent.Entities.Customer.MeterInfo> MeterInfos
        {
            get { return MeterInfoRepository.Entities; }
        }

        public OperationResult Insert(Domain.ViewModels.Maintenance.RepairRecord.RepairRecordModel model)
        {
           // model.tempLiuShui = RepairRecordService.RepairRecords.Where(t).Max(t => t.Liu_SH).ToString();
         //   GetLiuSModel(model);
            var entity = new RepairRecord
            {
                Meter_No = model.Meter_No != 0 ? model.Meter_No : 0,
                Liu_SH = model.Liu_SH,
                CreateDT =  DateTime.Now,
                Cao_ZY = model.Cao_ZY,
                Bei_Z = model.Bei_Z,
                Jian_XYY = model.Jian_XYY,
                Chu_LZT = false
            };
            RepairRecordRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public void GetLiuSModel(Domain.ViewModels.Maintenance.RepairRecord.RepairRecordModel model)
        {
            
            var strDt = DateTime.Now.Date.ToString("yyyyMMdd");

            model.Liu_SH = RepairRecordRepository.Entities.Where(t => t.Liu_SH.Contains(strDt)).Max(t => t.Liu_SH);
            if ((model.Liu_SH == "0") || (model.Liu_SH == null))
            {
                model.Liu_SH = strDt + "0001";
            }
            else
            {
                Int64 ints = Int64.Parse(model.Liu_SH);
                ints = ints + 1;
                model.Liu_SH = ints.ToString();
            }
        }

        public void ChangeState(Domain.ViewModels.Maintenance.RepairRecord.RepairRecordModel model)
        {

            var entity = MeterInfos.FirstOrDefault(t => t.Id == model.Meter_No);
            {


                entity.ReviseDT = DateTime.Now;
            };

            MeterInfoRepository.Update(entity);
          
        }


        public OperationResult Update(Domain.ViewModels.Maintenance.RepairRecord.RepairRecordModel model)
        {
            var entity = RepairRecords.FirstOrDefault(t => t.Id == model.Id);
            {

                entity.Chu_LZT = true;
                entity.ReviseDT = DateTime.Now;
                entity.Bei_Z = model.Bei_Z;
                entity.Jian_XYY = model.Jian_XYY;

            };
            RepairRecordRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "修改成功");
        }

        public Componnent.Common.OperationResult Delete(int Id)
        {
            var entity = RepairRecordRepository.Entities.FirstOrDefault(t => t.Id == Id);
            if (entity != null)
            {
                RepairRecordRepository.Delete(entity);
            }
            return new OperationResult(OperationResultType.Success, "删除成功");
        }
    }
}


